<template>
  <div class="city-container">
    <div v-if="activeValue" class="city-list">
      <span class="active">
        {{ activeValue }}
        <img src="@/static/image/region_cha.png" @click="cancel" />
      </span>
    </div>
    <div v-else class="city-list">
      <span @click="selectCity(-1,{})" :class="current == -1 ? 'active' : ''">全部</span>
      <span
        :class="current == index ? 'active' : ''"
        v-for="(item,index) in cityArr"
        :key="index"
        @click="selectCity(index,item)"
      >{{ item.value }}</span>
    </div>
  </div>
</template>
<script>
export default {
  name: "City",
  props: {
    activeValue: {
      type: String,
      default: ""
    },
    cityArr: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      current: -1 //
    };
  },
  methods: {
    cancel() {
      this.current = -1;
      this.$emit("cancel");
    },
    selectCity(index, item) {
      this.current = index;
      this.$emit("selectCity", item);
    }
  }
};
</script>
<style lang="less" scoped>
.city-container {
  width: 100%;
  .city-list {
    width: 100%;
    overflow: hidden;
    max-height: 32px;
    span {
      display: inline-block;
      font-size: 14px;
      padding: 2px 6px;
      margin: 3px 14px;
      background-color: #fff;
      color: #333;
      position: relative;
      overflow: hidden;
      border-radius: 4px;
      img {
        width: 16px;
        position: absolute;
        right: -2px;
        top: -2px;
      }
    }
    .active {
      background-color: #44a2ff;
      color: #fff;
      position: relative;
      overflow: hidden;
      border-radius: 4px;
      img {
        width: 16px;
        position: absolute;
        right: -2px;
        top: -2px;
      }
    }
  }
}
</style>